package com.base.mine.presenter;

import android.app.Activity;

import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.mine.view.ChangePwView;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/10/29.
 */

public class ChangePwPresenter extends BasePresenter<ChangePwView>{
    public ChangePwPresenter(ChangePwView view, Activity activity) {
        super(view, activity);
    }

    public void changePw(String orldPw, final String newPw, String confimPw, final String account){
        if (!newPw.equals(confimPw)){
            ToastUtil.show("确认密码和新密码不一致");
            return;
        }
        Map<String,String> param = new HashMap<>();
        param.put("oldPwd",orldPw);
        param.put("newPwd",newPw);
        mView.showPb();
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-auth/profile/resetPwd").content(param)
                .build().enqueue(String.class, new CallBackImpl() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(Object o) {
                        mView.hidePb();
                        SpUtil.setUserPw(newPw);
                        mView.showMes("保存成功");
                        Set<String> strings = new HashSet<>();
                        strings = SpUtil.getStringSet(strings);
                        Iterator<String> it_b=strings.iterator();
                        while(it_b.hasNext()){
                            String a=it_b.next();
                            if (a.contains(account)) {
                                it_b.remove();
                            }
                        }
//                        for (String str : strings) {
//                           if (str.contains(account)){
//                               strings.remove(str);
//                           }
//                        }
                        mActivity.finish();
                    }
                });
        mCompositeDisposable.add(enqueue);


    }
}
